<template>
  <div class="app-container">
    <el-card v-show="showSearch" style="margin-bottom: 20px">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="请假类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择请假类型" clearable size="small">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button-group>
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-button-group>
      </el-col>
      <el-col :span="1.5">
        <el-button-group>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            :disabled="single || buttonDisabled.modify"
            @click="handleUpdate"
          >修改
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-delete"
            size="mini"
            :disabled="single || buttonDisabled.delete"
            @click="handleDelete"
          >删除
          </el-button>
        </el-button-group>
      </el-col>
      <el-col :span="1.5">
        <el-button-group>
          <el-button
            type="primary"
            icon="el-icon-finished"
            size="mini"
            :disabled="single || buttonDisabled.submit"
            @click="handleSubmit"
          >提交
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-monitor"
            size="mini"
            :disabled="single"
            @click="handleDetailSchedule"
          >流程跟踪
          </el-button>
        </el-button-group>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaveApplicationList" @current-change="handleCurrentChange"
              height="calc(100vh - 252px)" highlight-current-row @row-dblclick="rowDblClick($event)">
      <el-table-column label="请假编号" align="center" prop="id" width="100"/>
      <el-table-column label="请假类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="标题" align="left" prop="title" min-width="150"/>
      <el-table-column label="原因" align="left" prop="reason" min-width="250"/>
      <el-table-column label="开始时间" align="center" prop="leaveStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="leaveEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="业务状态" min-width="90" prop="status">
        <template slot-scope="scope">
          <status-value-to-text :row="scope.row" type="highlight"></status-value-to-text>
        </template>
      </el-table-column>
<!--      操作列方式-->
<!--      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作" width="220">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            :class="!statusUtil.canHandle(scope.row)  ?'disabled-button':''"-->
<!--            :disabled="!statusUtil.canHandle(scope.row) "-->
<!--            icon="el-icon-finished"-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="handleSubmit(scope.row)"-->
<!--          >提交-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

    <edit ref="editDialog" @refreshDataList="getList"></edit>
    <detail ref="detailDialog" @refreshDataList="getList"></detail>
    <BpmnViewer ref="bpmnViewer" @refreshDataList="getList"></BpmnViewer>
  </div>
</template>

<script>
  import { listLeaveApplication, delLeaveApplication, exportLeaveApplication, submit } from '@/api/example/leaveappr/base'
  import StatusValueToText from '@/components/StatusValueToText'
  import BpmnViewer from '@/components/BpmnViewer/index'
  import Edit from './edit'
  import Detail from './detail'

  export default {
    name: 'LeaveApplication',
    components: { Edit, Detail, StatusValueToText, BpmnViewer },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        id: '',
        // 非单个禁用
        single: true,
        // 当前行数
        currentRow: null,
        // 按钮禁用组
        buttonDisabled: {
          // 提交禁用
          submit: true,
          // 修改禁用
          modify: true,
          // 删除禁用
          delete: true,
        },
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 请假申请表格数据
        leaveApplicationList: [],
        // 请假类型字典
        typeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: null,
          title: null,
          reason: null,
          leaveStartTime: null,
          leaveEndTime: null,
          instanceId: null,
          state: null
        },
      }

    },
    created() {
      this.getList()
      this.getDicts('sys_notice_type').then(response => {
        this.typeOptions = response.data
      })
    },
    methods: {
      /** 查询请假申请列表 */
      getList() {
        this.loading = true
        listLeaveApplication(this.queryParams).then(response => {
          this.leaveApplicationList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 请假类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type)
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      //选中数据
      handleCurrentChange(val) {
        if (val) {
          this.id = val.id
          // 单个值禁用
          if (this.id) {
            this.single = false
          }
          // 按钮组禁用
          this.buttonDisabled.submit = !this.statusUtil.canHandle(val)
          this.buttonDisabled.modify = !this.statusUtil.canHandle(val)
          this.buttonDisabled.delete = !this.statusUtil.canDelete(val)
          // 获取当前选中行
          this.currentRow = val
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs['editDialog'].reset()
        this.$refs['editDialog'].open = true
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs['editDialog'].reset()
        this.$refs['editDialog'].open = true
        const id = row.id || this.id
        this.$refs['editDialog'].init(id)
      },
      /** 查看按钮操作 */
      handleDetail(row) {
        this.$refs['detailDialog'].reset()
        this.$refs['detailDialog'].open = true
        const id = row.id || this.id
        this.$refs['detailDialog'].init(id)
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.id
        this.$confirm('是否确认删除请假申请编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delLeaveApplication(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有请假申请数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportLeaveApplication(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      },
      /** 双击查看 */
      rowDblClick(row) {
        this.handleDetail(row)
        // this.checkTheSchedule(row)
      },
      /** 提交按钮操作 */
      handleSubmit(row) {
        if (this.currentRow) {
          let row = this.currentRow
          this.$confirm('确定提交编号为' + row.id + '的申请吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            return submit(row)
          }).then((response) => {
            if (response.code === 200) {
              this.getList()
              this.msgSuccess('已提交到工作流')
            }
          }).catch(() => {
          })
        }
      },
      /** 进度查看 */
      handleDetailSchedule() {
        if (this.currentRow) {
          this.$refs['bpmnViewer'].open = true
          this.$refs['bpmnViewer'].init(this.currentRow.instanceId)
        }
      },
    }
  }
</script>
